package com.jsrj.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.jsrj.common.core.domain.AjaxResult;
import com.jsrj.common.utils.uuid.IdUtils;
import com.jsrj.domain.Advertorial;
import com.jsrj.domain.Banner;
import com.jsrj.domain.Image;
import com.jsrj.dto.AppAdvertorial;
import com.jsrj.dto.UsersDto;
import com.jsrj.model.WXAuth;
import com.jsrj.service.*;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/applet")
public class AppletControoller {
    @Autowired
    private IBannerService bannerService;
    @Autowired
    private IAdvertorialService advertorialService;
    @Autowired
    private IUsersService usersService;
    @Autowired
    private IImageService imageService;
    @Autowired
    private IShopService shopService;

    @Autowired
    private IPagesService pagesService;

    @Value("${wxmini.appid}")
    private String appid;
    @Value("${wxmini.secrey}")
    private String secrey;

    /**
     * 获取轮播图列表
     * @return
     */
    @GetMapping("/banner/selectAll")
    public AjaxResult selectAll() {

        return AjaxResult.success(bannerService.selectAll());
    }

    /**
     * 获取软文列表
     * @return
     */
    @GetMapping("/advertorial/selectAll")
    public AjaxResult list() {
        return AjaxResult.success(advertorialService.queryAllAdvertorials());
    }

    /**
     * 根据分类获取软文
     * @param shopId
     * @return
     */
    @GetMapping("/advertorial/select")
    public AjaxResult listShop(@RequestParam Long shopId) {
        return AjaxResult.success(advertorialService.queryAdvByShopId(shopId));
    }

    /**
     * 根据id获取软文信息
     * @param id
     * @return
     */
    @GetMapping("/advertorial/selectById")
    public AjaxResult queryAdvById(@RequestParam Long id){
        System.out.println(advertorialService.queryAdvById(id));
        return AjaxResult.success(advertorialService.queryAdvById(id));
    }
    /**
     * 用户登录获取token
     * @param wxAuth
     * @return
     */
    @PostMapping("/users/login")
    public AjaxResult getToken(@RequestBody WXAuth wxAuth){
        //微信官方提供用于获取session_key和openid的请求地址
        String appUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";
        //将url里的参数变量进行替换
        String url = appUrl.replace("APPID", appid).replace("SECRET", secrey).replace("JSCODE",wxAuth.getCode());
        //将返回的参数session_key和openid保存到变量用于生成token
        String res = HttpUtil.get(url);
        //将返回的参数解析成JSON对象
        JSONObject claims = JSON.parseObject(res);
        System.out.println(claims.get("errcode")!=null);
        if (claims.get("errcode")!=null){
            return  AjaxResult.error(claims.toJSONString());
        }
        //设置token的过期时间为20分钟
        Date expirationDate = new Date(System.currentTimeMillis() + 20*60 * 1000);
        //使用Jwts生成token
        String token = Jwts.builder().setClaims(claims).setExpiration(expirationDate).signWith(SignatureAlgorithm.HS512, secrey).compact();
        //将登录状态token
        return AjaxResult.success(token);
    }

    /**
     * 获取分类列表
     * @return
     */
    @GetMapping("/shop/queryAll")
    public AjaxResult getShop() {

        return AjaxResult.success(shopService.queryAll());
    }



}
